﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using UniMana.Utils.Data;
using System.Linq;
using System.Data.Linq;
using UniMana.Data;
namespace UniMana.Win.Systems
{
    public partial class FrmUserManager_SubSelectPermission : DevExpress.XtraEditors.XtraForm
    {
        public delegate void PassData(DataTable dtRule);
        public PassData passData;

        DataRow[] m_drListGroup;
        DataTable m_dtSys_User_Rule;
        public Guid m_guiUserKey;

        public FrmUserManager_SubSelectPermission(DataRow[] dr, Guid _guiUserKey)
        {
            InitializeComponent();
            m_drListGroup = dr;
            m_guiUserKey = _guiUserKey;
            LoadData();
        }

        public void LoadData()
        {
            m_dtSys_User_Rule = new DataTable();
            #region Tạo cấu trúc datable m_dtSys_User_Rule
            if (m_dtSys_User_Rule.Columns.Count == 0)
            {
                m_dtSys_User_Rule.Columns.Add("System_UserKey");
                m_dtSys_User_Rule.Columns.Add("System_RuleKey");

                m_dtSys_User_Rule.Columns.Add("RuleID");
                m_dtSys_User_Rule.Columns.Add("RuleName");
                m_dtSys_User_Rule.Columns.Add("RuleNameVi");
                m_dtSys_User_Rule.Columns.Add("ParentID");
                m_dtSys_User_Rule.Columns.Add("Level");
                m_dtSys_User_Rule.Columns.Add("AllowAccess", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowAdd", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowDelete", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowEdit", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowExport", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowImport", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowPrint", System.Type.GetType("System.Boolean"));
                m_dtSys_User_Rule.Columns.Add("AllowSave", System.Type.GetType("System.Boolean"));
            }

            #endregion

            if(m_drListGroup!=null)
            {
                for (int i = 0; i < m_drListGroup.Length; i++)
                {
                    DataTable dtGr = System_RuleData.RuleByGroup(new DBMain(), Convert.ToInt16(m_drListGroup[i][0])).ToDataTable();

                    foreach (DataRow row in dtGr.Rows)
                    {
                        DataRow dRow = (from DataRow r in m_dtSys_User_Rule.Rows where r["RuleID"].ToString().ToUpper() == row["RuleID"].ToString().ToUpper() select r).FirstOrDefault();

                        if (dRow == null)
                        {
                            m_dtSys_User_Rule.Rows.Add(new object[] { 
                            "", //Để trống userkey
                            row["System_RuleKey"],
                            row["RuleID"], 
                            row["RuleName"],
                            row["RuleNameVi"], 
                            (string)row["ParentID"]!="" ?  row["ParentID"] : m_drListGroup[i][0].ToString(),  
                                row["Level"], 
                               row["AllowAccess"],
                               row["AllowAdd"],
                             row["AllowDelete"],
                             row["AllowEdit"],
                             row["AllowExport"],
                           row["AllowImport"],
                            row["AllowPrint"],
                          row["AllowSave"] });
                           
                        }
                        else 
                        {
                            if ((string)row["ParentID"]=="")
                            {
                                dRow["ParentID"] = "common";
                            }
                        }

                    }

                    m_dtSys_User_Rule.Rows.Add(new object[] { 
                        "", 
                        m_drListGroup[i][0].ToString(),
                        m_drListGroup[i][0].ToString(), 
                        m_drListGroup[i][0].ToString(),
                        m_drListGroup[i][1].ToString(), //row["RuleNameVi"], 
                        "",//row["ParentID"]!="" ?  row["ParentID"] : m_dr[i][0].ToString(),  
                        -1, 
                        true,
                        true, 
                        true, 
                        true, 
                        true, 
                        true, 
                        true, 
                        true 
                    });
                }

                #region Thêm nhóm chứa các chức năng chung của nhiều group
                if (m_drListGroup.Length > 1)
                {
                    m_dtSys_User_Rule.Rows.Add(new object[] { 
                        "", 
                        "common",
                        "common", 
                        "common",
                        "Cùng thuộc nhiều nhóm", //row["RuleNameVi"], 
                        "",//row["ParentID"]!="" ?  row["ParentID"] : m_dr[i][0].ToString(),  
                        -1, 
                        true,
                        true, 
                        true, 
                        true, 
                        true, 
                        true, 
                        true, 
                        true });
                }
                #endregion

                tltPermission.DataSource = m_dtSys_User_Rule;
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            //if (m_drListGroup != null)
            //{
            //    for (int i = 0; i < m_drListGroup.Length; i++)
            //    {
            //        //System_User_GroupInfo sugi = new System_User_GroupInfo(m_guiUserKey, int.Parse(m_drListGroup[i][0].ToString()));
            //        //sugi.Save();
            //    }
            //}
            passData(m_dtSys_User_Rule);
            Close();
        }
    }
}